package server.startup;


import server.Lifecycle;
import server.LifecycleListener;
import server.Mapper;
import server.connector.http.HttpConnector;
import server.core.*;

public class BootStrap {
    public static void main(String[] args) {
        HttpConnector connector = new HttpConnector();

        SimpleWrapper wrapper1 = new SimpleWrapper();
        wrapper1.setName("Primitive");
        wrapper1.setServletClass("PrimitiveServlet");
        SimpleWrapper wrapper2 = new SimpleWrapper();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");

        SimpleContext context = new SimpleContext();
        context.addChild(wrapper1);
        context.addChild(wrapper2);

        Mapper mapper = new SimpleContextMapper();
        mapper.setProtocol("http");
        context.addMapper(mapper);

        context.addServletMapping("/Primitive", "Primitive");
        context.addServletMapping("/Modern", "Modern");

        LifecycleListener listener = new SimpleContextLifecycleListener();
        ((Lifecycle) context).addLifecycleListener(listener);

        SimpleLoader loader = new SimpleLoader();
        context.setLoader(loader);

        connector.setContainer(context);
        try {
            //连接器初始化,创建ServerSocket
            connector.initialize();
            //连接器创建好初始的处理器数后启动自己的线程
            connector.start();
            ((Lifecycle) context).start();
            System.in.read();
            ((Lifecycle) context).stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
